home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / bin / setupcon < prev    next >
Text File  |  2009-10-02  |  9KB  |  356 lines

  1. #!/bin/sh
  2.  
  3. #     setupcon -- setup the font and keyboard on the Linux console
  4. #     Copyright ┬⌐ 1999,2000,2001,2002,2003,2006 Anton Zinoviev
  5.  
  6. #     This program is free software; you can redistribute it and/or modify
  7. #     it under the terms of the GNU General Public License as published by
  8. #     the Free Software Foundation; either version 2 of the License, or
  9. #     (at your option) any later version.
  10.  
  11. #     This program is distributed in the hope that it will be useful,
  12. #     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #     GNU General Public License for more details.
  15.  
  16. #     If you have not received a copy of the GNU General Public License
  17. #     along with this program, write to the Free Software Foundation, Inc.,
  18. #     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. ###########################################################################
  21.  
  22.  
  23. # The same as /usr/bin/which - in order to make "which" available before
  24. # /usr is mounted
  25. which () {
  26.     local IFS
  27.     IFS=:
  28.     for i in $PATH; do
  29.     if [ -x "$i/$1" ]; then
  30.         echo "$i/$1"
  31.         return 0
  32.     fi
  33.     done
  34.     return 1
  35. }
  36.  
  37. while [ "$1" ]; do
  38.     case "$1" in
  39.     -k|--keyboard-only)
  40.         keyboard_only=yes
  41.         ;;
  42.     -f|--font-only)
  43.         font_only=yes
  44.         ;;
  45.     -v|--verbose)
  46.         verbose_option=yes
  47.         ;;
  48.     --force)
  49.         force=yes
  50.         ;;
  51.     --save)
  52.         save=yes
  53.         ;;
  54.     --save-only)
  55.         force=yes
  56.         save=yes
  57.         save_only=yes
  58.         ;;
  59.     -h|--help)
  60.         cat >&2 <<EOF
  61. Usage: setupcon [OPTION] [VARIANT]
  62. Sets up the font and the keyboard on Linux console.
  63.  
  64.   -k, --keyboard-only  setup the keyboard only, do not setup the font
  65.   -f, --font-only      setup the font only, do not setup the keyboard
  66.       --force          do not check whether we are on the console
  67.   -v, --verbose        explain what is being doing, try it if s.t. goes wrong
  68.       --save           copy the font and the ACM in /etc/console-setup,
  69.                          update /etc/console-setup/cached.kmap.gz
  70.       --save-only      only save; don't setup keyboard/font immediately
  71.                          (implies --force)
  72.   -h, --help           display this help and exit
  73.  
  74. If VARIANT is not specified setupcon looks for the configuration files
  75. (in this order) ~/.console-setup and /etc/default/console-setup.  When
  76. a VARIANT is specified then setupcon looks for the configuration files
  77. ~/.console-setup.VARIANT and /etc/default/console-setup.VARIANT.
  78. EOF
  79.         exit 0
  80.         ;;
  81.     -*)
  82.         echo "setupcon: Unrecognised option $1" >&2
  83.         exit 1
  84.         ;;
  85.     *)
  86.         if [ -z "$VARIANT" ]; then
  87.         VARIANT="$1"
  88.         else
  89.         echo "setupcon: Two variants specified: $VARIANT and $1" >&2
  90.         exit 1
  91.         fi
  92.         ;;
  93.     esac
  94.     shift
  95. done
  96.  
  97. if [ "$VARIANT" ]; then
  98.     USER_CONFIG=${HOME}/.console-setup."$VARIANT"
  99.     MAIN_CONFIG=/etc/default/console-setup."$VARIANT"
  100. else
  101.     USER_CONFIG=${HOME}/.console-setup
  102.     MAIN_CONFIG=/etc/default/console-setup
  103. fi
  104.  
  105. if [ -f "$USER_CONFIG" ]; then
  106.     CONFIG="$USER_CONFIG"
  107.     save=
  108. elif [ -f "$MAIN_CONFIG" ]; then
  109.     CONFIG="$MAIN_CONFIG"
  110. else
  111.     echo "setupcon: None of $MAIN_CONFIG nor $USER_CONFIG exists." >&2
  112.     exit 1
  113. fi
  114.  
  115. . "$CONFIG"
  116.  
  117. if [ -d /lib/debian-installer ]; then
  118.     CHARMAP=UTF-8
  119. fi
  120.  
  121. if [ "$verbose_option" = yes ]; then
  122.     VERBOSE_OUTPUT=yes
  123. fi
  124. if [ "$VERBOSE_OUTPUT" = yes ]; then
  125.     verbose=''
  126. else
  127.     verbose='>/dev/null 2>&1'
  128. fi
  129.  
  130. if which ckbcomp-mini >/dev/null && [ "$CHARMAP" != UTF-8 ]; then
  131.     CHARMAP=UTF-8
  132.     if [ "$VERBOSE_OUTPUT" = yes ]; then
  133.     echo Only UTF-8 is supported by console-setup-mini
  134.     fi
  135. fi
  136.  
  137. if [ "$force" != yes ]; then
  138.     case `readlink /proc/self/fd/2` in
  139.     /dev/tty[0-9]*|/dev/vc/[0-9]*|/dev/console)
  140.         ;;
  141.     *)
  142.         if [ "$VERBOSE_OUTPUT" = yes ]; then
  143.         echo We are not on the Linux console, exiting.
  144.         fi
  145.         exit 0 
  146.         ;;
  147.     esac
  148. fi
  149.  
  150. #-----------------------#
  151. #       OUTPUT          #
  152. #-----------------------#
  153.  
  154. if [ "$keyboard_only" != yes ] && [ "$ACTIVE_CONSOLES" ]; then
  155.     # Setup unicode/non-unicode mode
  156.     if [ "$save_only" != yes ]; then
  157.     for console in $ACTIVE_CONSOLES; do
  158.         [ -w $console ] || continue
  159.         if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM$CHARMAP" ]; then
  160.         printf '\033%%G' >$console
  161.         else
  162.         printf '\033%%@' >$console
  163.         fi
  164.     done
  165.     fi
  166.     
  167.     # Load the font
  168.     if [ -z "$FONT" ]; then
  169.     if which ckbcomp-mini >/dev/null; then
  170.         FONT=$(echo `ls /usr/share/consolefonts/$CODESET-*.psf.gz \
  171.                 2>/dev/null`)
  172.         FONT=${FONT%% *}
  173.     elif [ "$FONTFACE" ] && [ "$FONTSIZE" ]; then
  174.         FONT="$CODESET-$FONTFACE$FONTSIZE.psf.gz"
  175.         case "$FONTSIZE" in
  176.         *x*)
  177.             bigfont=yes
  178.             ;;
  179.         *)
  180.             bigfont=no
  181.             ;;
  182.         esac
  183.     fi
  184.     fi
  185.     LOADFONTS=''
  186.     for f in $FONT; do
  187.     if [ -f "$f" ]; then
  188.         LOADFONTS="$LOADFONTS $f"
  189.         else
  190.         for dir in \
  191.         /usr/local/share/consolefonts \
  192.         /usr/share/consolefonts \
  193.         /etc/console-setup \
  194.         FONT_IS_MISSING
  195.         do
  196.         if [ -f "$dir/${f##*/}" ]; then
  197.             LOADFONTS="$LOADFONTS $dir/${f##*/}"
  198.             break
  199.         fi
  200.         if [ "$dir" = FONT_IS_MISSING ]; then
  201.             echo setupcon: Warning: the font ${f##*/} does not exists! >&2
  202.         fi
  203.         done
  204.     fi
  205.     done
  206.     
  207.     for f in $LOADFONTS; do
  208.     if \
  209.         [ "$save" = yes ] \
  210.         && [ "${f%/*}" != /etc/console-setup ]
  211.     then
  212.         cp "$f" /etc/console-setup/
  213.     fi
  214.     done
  215.     
  216.     
  217.     if [ "$save_only" != yes ] && [ "$LOADFONTS" ]; then
  218.     for console in $ACTIVE_CONSOLES; do
  219.         [ -w $console ] || continue
  220.         if which consolechars >/dev/null; then
  221.         if [ "$bigfont" = yes ]; then
  222.             echo "setupcon: The consolechars utility from the console-setup font can load only fonts with 8 pixel width matrix.  Please install the setfont utility from the kbd package." >&2
  223.         fi
  224.         eval consolechars -v --tty=$console -f $LOADFONTS $verbose
  225.         elif which setfont >/dev/null; then
  226.         eval setfont -v -C $console $LOADFONTS $verbose
  227.         fi
  228.     done
  229.     fi
  230.     
  231.     # Load the ACM
  232.     if [ ! -f "$ACM" ]; then
  233.     for dir in \
  234.         /usr/local/share/consoletrans \
  235.         /usr/share/consoletrans \
  236.         /etc/console-setup 
  237.     do
  238.         if [ -f "$dir/$CHARMAP.acm.gz" ]; then
  239.         ACM="$dir/$CHARMAP.acm.gz"
  240.         break
  241.         fi
  242.     done
  243.     fi
  244.     if [ -f "$ACM" ]; then
  245.     if \
  246.         [ "$save" = yes ] \
  247.         && [ "${ACM%/*}" != /etc/console-setup ]
  248.     then
  249.         cp "$ACM" /etc/console-setup/
  250.     fi
  251.     else
  252.     ACM="$CHARMAP.acm.gz"
  253.     fi
  254.     if [ "$save_only" != yes ] && [ "$CHARMAP" != UTF-8 ]; then
  255.     for console in $ACTIVE_CONSOLES; do
  256.         [ -w $console ] || continue
  257.         if which consolechars >/dev/null; then
  258.         eval consolechars -v --tty=$console --acm "$ACM" $verbose
  259.         elif which setfont >/dev/null; then
  260.         eval setfont -v -C $console -m "$ACM" $verbose
  261.         fi        
  262.     done
  263.     fi
  264. fi
  265.  
  266. #-----------------------#
  267. #        INPUT          #
  268. #-----------------------#
  269.  
  270. if [ "$font_only" != yes ] && [ "$ACTIVE_CONSOLES" ]; then
  271.     # On Mac PPC machines, we may need to set kernel vars first.  We need
  272.     # to mount /proc to do that, but we need it set up before sulogin may
  273.     # be run in checkroot, which will need the keyboard to log in...
  274.     # This code was borrowed from the keymap.sh script of console-common
  275.     # Copyright ┬⌐ 2001 Yann Dirson
  276.     # Copyright ┬⌐ 2001 Alcove http://www.alcove.fr/
  277.     if [ "$save_only" != yes ] && \
  278.        [ -x /sbin/sysctl ] && [ -r /etc/sysctl.conf ]; then
  279.     if grep -v '^\#' /etc/sysctl.conf | grep -q keycodes ; then
  280.         grep keycodes /etc/sysctl.conf | grep -v "^#" \
  281.         | while read d ; do
  282.               /sbin/sysctl -w $d 2> /dev/null || true
  283.               done
  284.     fi
  285.     fi
  286.     
  287.     if [ "$save_only" != yes ]; then
  288.     for console in $ACTIVE_CONSOLES; do
  289.         [ -w $console ] || continue
  290.         if which kbd_mode >/dev/null; then
  291.         if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM" ]; then
  292.             kbd_mode -u <$console
  293.         else
  294.             kbd_mode -a <$console
  295.         fi
  296.         fi
  297.     done
  298.     fi
  299.     
  300.     if which loadkeys >/dev/null; then
  301.     if [ "$XKBMODEL" != SKIP ] && which ckbcomp >/dev/null; then
  302.         if [ "$CHARMAP" != UTF-8 ]; then
  303.         acm_option="-charmap $CHARMAP"
  304.         else
  305.         acm_option=''
  306.         fi
  307.  
  308.         if [ "$XKBRULES" ]; then
  309.         rules_option="-rules $XKBRULES"
  310.         else
  311.         rules_option=''
  312.         fi
  313.  
  314.         if \
  315.         [ "$VARIANT" = '' ] \
  316.         && which gzip >/dev/null \
  317.         && [ -d /usr/share/X11/xkb ] # /usr is mounted
  318.         then
  319.         if \
  320.             [ ! -f /etc/console-setup/cached.kmap.gz ] \
  321.             || [ /etc/console-setup/cached.kmap.gz \
  322.                    -ot /etc/default/console-setup ]
  323.         then
  324.             ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
  325.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
  326.             | gzip -9 >/etc/console-setup/cached.kmap.gz
  327.         fi
  328.         fi
  329.  
  330.         if [ "$save_only" != yes ]; then
  331.         if \
  332.             [ "$VARIANT" = '' ] && [ "$CONFIG" != "$USER_CONFIG" ] \
  333.             && [ -f /etc/console-setup/cached.kmap.gz ] \
  334.             && [ ! /etc/console-setup/cached.kmap.gz \
  335.                    -ot /etc/default/console-setup ]
  336.         then
  337.             eval loadkeys /etc/console-setup/cached.kmap.gz $verbose
  338.         else
  339.             ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
  340.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
  341.             | eval loadkeys $verbose
  342.         fi
  343.         fi
  344.         if which gzip >/dev/null && [ "$save" = yes ]; then
  345.         ckbcomp $acm_option $rules_option -model "$XKBMODEL" \
  346.             "$XKBLAYOUT" "$XKBVARIANT" "$XKBOPTIONS" \
  347.             | gzip -9 >/etc/console-setup/cached.kmap.gz
  348.         fi
  349.     elif [ "$save_only" != yes ] && \
  350.          [ -f /etc/console-setup/cached.kmap.gz ]; then
  351.         eval loadkeys /etc/console-setup/cached.kmap.gz $verbose
  352.     fi
  353.     fi
  354. fi
  355.  
  356.